<template>
    <div>
        <!-- 开票情况:rzKpqk-->
        <div class="w_100 headTips d_f j_c_s_b">
            <div class="d_f a_i_c">
                <div class="line"></div>
                {{ name }}
            </div>
            <div class="isClose" @click="close">
                <el-icon v-if="type == 'smallForm'">
                    <CloseBold />
                </el-icon>
            </div>
            <el-switch v-if="type == 'bigForm'" @change="change" v-model="status"
                style="--el-switch-on-color: #13ce66;" />
            <el-button type="primary" v-if="type == 'useForm' && isReset == 1" @click="clearForm">清除表单</el-button>
        </div>
        <div class="formBox rzKpqk">
            <myform :fields="fields" :rules="rules" ref="form1" @handleSelect="handleSelect" :model="model.condition"
                :options="options" class="m_t_16">
            </myform>
            <div class="segment" v-if="fields1.length > 0"></div>
            <div class="formArrow d_f a_i_c m_t_30" v-if="fields1.length > 0">
                <img src="../../assets/formArrow.png" alt="">
                <span>{{ subTitle || '新加字段' }}</span>
            </div>
            <div class="acceptance_form" v-if="fields1.length > 0">
                <myform :fields="fields1" :model="model.condition" :options="options" class="m_t_16">
                </myform>
            </div>
        </div>
    </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, onBeforeMount, ref } from 'vue'
import { rzKpqkType } from './data'
import { CloseBold } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { formGet, unitFormSet } from '../../api/system/flowPath'
import { listByParentArr } from '../../api/login'
import { Message } from '../../utils/resetMessage'
import { copy } from '../../utils/date'
const model = {
    condition: {
        conditionStatus: '',//是否开具发票 0是 1否
        conditionDate: '',//开具日期
        isReview: 1
    },
}
export default defineComponent({
    name: '',
    props: {
        type: {
            type: String,
            default: '',
        },
        unitId: {
            type: [String, Number],
            default: '',
        },
        isEnabled: {
            type: Number,
            default: '',
        },
        nodeObj: {
            type: Object,
            default: {}
        },
        vars: {
            type: Object,
            default: {}

        },
        isAudit: {
            type: Boolean,
            default: true
        }
    },
    components: {
        CloseBold
    },
    setup(props: any) {
        const data: rzKpqkType = reactive({
            name: '开票情况',
            status: true,
            options: {
                conditionStatus: [
                    {
                        name: '是',
                        id: '1'
                    },
                    {
                        name: '否',
                        id: '0'
                    },
                ],
            },
            model: copy(model),
            subTitle: '',
            fields: [

                {
                    label: "是否已开具发票",
                    prop: 'conditionStatus',
                    placeholder: '请选择是否已开具发票',
                    type: 'select',
                    bindLabel: 'name',
                    bindValue: 'id',
                    labelWidth: '150px',
                    disabled: false,
                },
                {
                    label: "开票日",
                    prop: 'conditionDate',
                    placeholder: '请选择开票日',
                    type: 'date',
                    labelWidth: '150px',
                    disabled: false,
                },

            ],
            fields1: [],
            rules: {
                conditionStatus: [
                    { required: true, message: '请选择是否开具发票', trigger: 'change' },
                ],
                conditionDate: [
                    { required: true, message: '请选择开票日', trigger: 'change' },
                ],
            },
            id: '',
            isReset: 2,
        })
        const router = useRouter()
        const form1 = ref()
        onBeforeMount(() => {
            methods.getData()
            data.status = props.isEnabled == 0 ? false : true
        })
        const methods = {
            //获取自定义表单
            getData() {
                formGet({ tag: 'rzKpqk' }).then((res: any) => {
                    data.name = res.name
                    data.id = res.id
                    data.isReset = res.isReset
                    if (!res.formStruct || res.formStruct == 'null' || res.formStruct == '{}') return
                    data.subTitle = res.subTitle
                    let formStruct = JSON.parse(res.formStruct)
                    formStruct.formFields.map((item:any)=>{
                        data.model.condition[item.prop]=''
                    })
                    data.fields1 = formStruct.formFields
                    data.fields.map((item: any) => {
                        item.disabled = props.isAudit
                    })
                    data.fields1.map((item: any) => {
                        item.disabled = props.isAudit
                    })
                    if (props.vars) {
                        const localData = props.vars
                        for (let key in data.model.condition) {
                           if(localData.condition[key]!=null){
                            data.model.condition[key] = localData.condition[key]
                           }
                        }
                        if(data.model.condition.conditionStatus!=null){
                            data.model.condition.conditionStatus = String(data.model.condition.conditionStatus)
                        }
                    }
                    methods.handleSelect('conditionStatus')
                    let arr = formStruct.formFields.filter((item: any) => {
                        return item.type == 'select'
                    })
                    let ids = arr.map((item: any) => {
                        return item.prop
                    })
                    if (ids.length == 0) return
                    listByParentArr({ ids: ids }).then((res: any) => {
                        ids.map((item: any) => {
                            data.options[item] = res[item]
                        })
                    })
                })
            },
            close() {
                router.push('/system/flowPath/childForm/index')
            },
            //设置表单的启用跟停用
            change() {
                unitFormSet({ isEnabled: data.status ? 1 : 0, id: data.id, unitId: props.unitId }).then(() => {
                    Message({ type: 'success', message: '设置成功' })
                }).catch(() => {
                    data.status = !data.status
                })
            },
            //清除表单数据
            clearForm() {
                data.model = copy(model)
            },
            resultData() {
                return new Promise((resolve, reject) => {
                    form1.value.verification().then((res) => {
                        resolve(data.model)
                    }).catch(() => {
                        reject(false)
                    })

                })
            },
            saveData() {
                return new Promise((resolve, reject) => {
                    resolve(data.model)
                })
            },
            handleSelect(prop: string) {
                console.log(prop);
                if(prop=='conditionStatus'){
                 if(data.model.condition.conditionStatus=='1'){
                  data.rules.conditionDate[0].required=true
                  data.fields[1].show=false
                 }else{
                    data.rules.conditionDate[0].required=false
                    data.model.condition.conditionDate=''
                    data.fields[1].show=true
                    
                 }
                }
            }
        }
        return {
            ...toRefs(data),
            ...methods, form1
        }
    }
})
</script>
<style lang='scss'>
.rzKpqk {
    .el-form-item {
        width: 50% !important;
        margin-right: 0px !important;

        .el-form-item__content {
            width: calc(100% - 150px) !important;

            .el-input,
            .el-cascader,
            .el-select {
                width: 100% !important;
            }
        }

    }
}
</style>